package com.itheima.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo15")
public class ServletDemo15 extends HttpServlet {
    private String ETAG="sdhgjfsdjf";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //通过last-modified响应头设置资源的过期时间
        //后续这个资源的过期时间会通过is-modified-since请求携带回服务器进行过期判断
        resp.setDateHeader("last-modified",System.currentTimeMillis()+60*1000*60);
        //通过响应头，给这个资源设置一个标记
        resp.setHeader("etag",ETAG);
        resp.getWriter().write("你");
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected long getLastModified(HttpServletRequest req) {
        //获取之气前浏览器缓存的那个资源的标记
        String oldEtag =req.getHeader("if-none-match");
        //将浏览器缓存资源的标记和当前最新的标记进行对比，如果比对不一致，直接返回-1
        if(oldEtag!=null&&!oldEtag.equals(ETAG)){
            return -1;
        }
        return System.currentTimeMillis();
    }
}
